package com.sniff.atp.modules.test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.BugRegister;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.beework.Project;
import com.sniff.atp.modules.test.service.DemandReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 需求登记 前端控制器
 * </p>
 *
 * @author macro
 * @since 2023-09-07
 */
@RestController
@RequestMapping("/test/demandReport")
@Api(value = "需求登记", tags = "需求登记")
public class DemandReportController {
    @Autowired
    private DemandReportService demandReportService;

    @ApiOperation("新增需求")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody DemandReport demandReport) {
        boolean success = demandReportService.create(demandReport);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改需求")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody DemandReport demandReport) {

        boolean success = demandReportService.update(demandReport.getDemandId(), demandReport);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取需求详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<DemandReport> getItem(@PathVariable Long id) {
        DemandReport demandReport = demandReportService.getById(id);
        return CommonResult.success(demandReport);
    }

    @ApiOperation("根据ID删除问题列表")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = demandReportService.removeById(id);

        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询问题")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<DemandReport>> list(@RequestBody DemandReport demandReport) {
        if(demandReport.getPageSize() == null){
            demandReport.setPageSize(10);
        }
        if(demandReport.getPageNum() == null){
            demandReport.setPageNum(1);
        }
        Page<DemandReport> menuList = demandReportService.list(demandReport, demandReport.getPageSize(), demandReport.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("按业务线查询beework项目")
    @RequestMapping(value = "/qryBeeworkProduct", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<Project>> qryBeeworkProduct(@RequestBody DemandReport demandReport) {
        if(demandReport.getDemandLine() == null){
            return CommonResult.failed("业务线不能为空");
        }
        List<Project> menuList = demandReportService.qryBeeworkProduct(demandReport);
        return CommonResult.success(menuList);
    }

}

